package com.firemoss.atonement.entitymanager.mapper
{
	import com.firemoss.atonement.entitymanager.EntityManager;
	import com.firemoss.atonement.entitymanager.IEntityManager;
	import com.firemoss.atonement.mapper.resolution.DefaultValueResolverFactory;
	import com.firemoss.atonement.mapper.resolution.IValueResolver;
	import com.firemoss.atonement.metadata.AtonementMetadata;
	import com.firemoss.atonement.metadata.IMetadataBuilder;
	
	import org.as3commons.reflect.Type;
	
	public class EntityManagerValueResolverFactory extends DefaultValueResolverFactory
	{
		private var resolver : IValueResolver
		private var entityManager : IEntityManager
		
		public function EntityManagerValueResolverFactory( entityManager : IEntityManager )
		{
			this.entityManager = entityManager
			resolver = new EntityValueResolver()
		}
		
		override public function create(type:Type, targetType:Type):IValueResolver
		{
			if ( type.clazz != null )
			{
				var md : AtonementMetadata = entityManager.metadataBuilder.buildMetadataForClass( type.clazz )
					
				if ( md.entityMetadata )
				{
					return resolver
				}
			}
			
			return super.create( type, targetType )
		}	
		
	}
}